package com.longy.desensitized.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longy.desensitized.mapper.UserMapper;
import com.longy.desensitized.model.User;
import com.longy.desensitized.vo.request.ResetPasswordRequest;
import com.longy.desensitized.vo.resp.UserResp;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.FieldDefaults;

/**
 * @author longy Created on 2022/11/7
 */
@Service
@CrossOrigin(origins = "*")
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Override
    public List<UserResp> getUserList() {
        //查询用户列表
        List<User> list = this.list();
//        //常规方式
//        list.forEach(e ->{
//            //1、获取需要脱敏的值
//            String mobile = e.getMobile();
//            //2、处理值（脱敏）
//            String desensitizedMobile = DesensitizedUtil.mobilePhone(mobile);
//            //3、获取需要脱敏的值
//            e.setMobile(desensitizedMobile);
//        });
        //转换为返回类
        List<UserResp> resp = BeanUtil.copyToList(list, UserResp.class);

        return resp;
    }

    @Override
    public String resetPassword(ResetPasswordRequest request) {
        User user = BeanUtil.copyProperties(request, User.class);
        boolean updRet = this.updateById(user);
        return updRet ? "true" : "false";
    }
}
